package com.cy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.cy.bfzcommons.mapper.CategoryMapper;
import com.cy.bfzcommons.pojo.Category;
import com.cy.bfzcommons.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void saveCategory(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> findPicture() {
        return categoryMapper.selectList(null);
    }

    //1.封装一个Map集合
    public Map<Integer,List<Category>> getMap(){
        Map<Integer,List<Category>> map = new HashMap<>();
        //1.查询所有的数据信息
        List<Category> list =  categoryMapper.selectList(null);
        //2.将List集合封装到Map中
        for (Category category : list ){
            //规则:  判断map中是否有key
            // 没有key???  该子级是第一个父级元素的孩子,应该声明父级并且将子级作为第一个子级保存
            // 有key??  找到父级的子级序列  将子级追加到序列中即可
            if (map.containsKey(category.getParent())){
                //获取父级的所有已知子级
                map.get(category.getParent()).add(category);


            }else {
                //没有父级
                List<Category> initList = new ArrayList<>();
                initList.add(category);
                map.put(category.getParent(),initList);
            }

        }
        return map;
    }

    @Override
    public List<Category> getCategory(Integer level) {
        Map<Integer,List<Category>> map = getMap();
        List<Category> catList = new ArrayList<>();
        if (level ==1){
            //获取一级商品分类信息
            return  map.get(0);
        }

        if (level == 2){

            return getLevel2(map);
        }

        if (level == 3){
            return  getLevel3(map);
        }
        return getLevel3(map);
    }

    private List<Category> getLevel3(Map<Integer, List<Category>> map) {
        //1.获取二级商品分类信息 1级里面套二级
        List<Category> list =  getLevel2(map);
        for (Category category : list){
            List<Category> list2 = category.getChildren();
            //根据2级查询3级信息
            if (list2 == null) continue;  //判断数据是否有效!
            for (Category category1:list2){
                category1.setChildren(map.get(category1.getCategoryId()));
            }
            //将3级已经封装完成  将二级封装到一级中
            category.setChildren(list2);
        }
        return list;
    }

    private List<Category> getLevel2(Map<Integer, List<Category>> map) {
        //1.获取1级商品分类信息
        List<Category> list = map.get(0);
        //2.封装2级菜单信息
        for (Category category : list){
            //如何获取该一级菜单的子级?
            category.setChildren(map.get(category.getCategoryId()));

        }
        return list;
    }

    @PutMapping
    @Override
    public void updateCategory(@RequestBody Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Integer categoryId,Integer level) {
        if (level == 3) {
            //如果是三级商品分类菜单则直接删除
            categoryMapper.deleteById(categoryId);
        }

        if (level == 2) {
            //需要删除的是2级菜单 , 关联删除3级菜单
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent", categoryId);
            categoryMapper.delete(queryWrapper);
            //删除2级菜单
            categoryMapper.deleteById(categoryId);
        }

        if (level == 1) {
            //1.查询二级分类信息  parent_id  =  一级id
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent", categoryId);
            //获取主键信息(第一列信息)
            List<Object> twoList = categoryMapper.selectObjs(queryWrapper);


            for (Object twoId : twoList) {

                //先删除3级
                QueryWrapper<Category> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("parent", twoId);
                categoryMapper.delete(queryWrapper2);

                //再删除2级
                Integer intTwoId = (Integer) twoId;
                categoryMapper.deleteById(intTwoId);
            }


            //最后删除一级
            categoryMapper.deleteById(categoryId);
        }
    }
}
